package com.lwq.weatherapp.util;

import java.io.Closeable;
import java.util.Map;

/**
 *  网络请求工具类
 */
public class HiNetUtil {

    /**
     *  拼接urlde
     *  @param url
     *  @param params
     *  @return
     */
    public static String buildParams(String url, Map<String, String> params){
        if (null == params) return url;
        StringBuilder builder = new StringBuilder(url);
        boolean isFirst = true;
        for (String key: params.keySet()) {
            String value = params.get(key);
            if (null != key && null != value) {
                //判断是否是第一次操作
                if (isFirst) {
                    isFirst = false;
                    builder.append("?");
                }else {
                    builder.append("&");
                }
                builder.append(key)
                        .append("=")
                        .append(value);
            }
        }
        return builder.toString();
    }

    //关闭
    public static void close(Closeable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
